"""
@File    :   lifespan.py
@Time    :   2024/01/07 20:00:53
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   注册生命周期事件
"""

from contextlib import asynccontextmanager

from fastapi import FastAPI

from src.lifespan.redis_use import RedisLifeSpan


@asynccontextmanager
async def register_lifespan(app: FastAPI):
    """注册生命周期事件

    Args:
        app (FastAPI): Fastapi实例,
    """
    # 挂载redis缓存连接池
    await RedisLifeSpan.cache_startup(app)
    # 挂载redis的Token连接池
    await RedisLifeSpan.token_startup(app)
    yield
    # 关闭挂载的redis缓存连接池
    await RedisLifeSpan.cache_shutdown(app)
    # 关闭挂载的redis的Token连接池
    await RedisLifeSpan.token_shutdown(app)
